#include "Game.hpp"
#include "ImageManager.hpp"
#include "BattleField.hpp"

Game::Game()
	: m_app(sf::VideoMode(640, 480, 32), "Skull Delta", sf::Style::Close)
{
	m_app.SetFramerateLimit(60);
}

Game::~Game()
{
	SpriteManager::destroy();
}

void Game::event()
{

    // manage each gameObject's events
	for(int i = 0; i < m_gameObjects.size(); i++)
	    m_gameObjects[i]->event(m_app.GetInput());

    // manage the window's events
	while (m_app.GetEvent(m_evt))
	{
	    // window closed
		if (m_evt.Type == sf::Event::Closed)
			m_app.Close();

        // on key stroke
        if (m_evt.Type == sf::Event::KeyPressed)
            switch(m_evt.Key.Code)
            {
                case sf::Key::Escape:
                    m_app.Close();
                    break;

                default:
                    break;
            }
	}
}

void Game::update()
{
    // update each gameObject
	for(int i = 0; i < m_gameObjects.size(); i++)
	    m_gameObjects[i]->update(m_app.GetFrameTime());

    // Player
	//m_player.update(m_app.GetFrameTime());

    // BattleField
	BF::get()->update(m_app.GetFrameTime());
}

void Game::show()
{
	m_app.Clear();
	/* Draw code here */

    // show BattleField
	BF::get()->show(m_app);

    // show each gameObject
	for(int i = 0; i < m_gameObjects.size(); i++)
	    m_gameObjects[i]->show(m_app);

	/* Draw code here */
	m_app.Display();
}

void Game::run()
{
    // initialize game elements
    Animation *Anim1 = new Animation("data/skullHead.png", 3, 0.5f, 3, 1);
    Anim1->setPosition(100, 100);

    m_gameObjects.push_back(Anim1);
    m_gameObjects.push_back(&m_player);

    while (m_app.IsOpened())
    {
		event();
		update();
		show();
    }
}
